<?php

namespace App\Core\Utils\TwHelper;

class Random
{

    /**
     * 获取随机码
     * @param type $number 位数 默认32位
     * @param type $type number：数字 char：字符a-zA-Z 默认：数字和字符
     * @return string
     */
    public static function getRandomPwd($number = 32, $type = '') {
        $chars = '';
        switch ($type) {
            case 'number':
                $chars = '0123456789';
                break;
            case 'char':
                $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            default : $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        }
        $str = '';
        for ($i = 0; $i < $number; $i++) {
            $str .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $str;
    }

    /**
     * MD5唯一码
     * @return type
     */
    public static function getUniqid($number = 24) {
        $str = uniqid(mt_rand(), 1);
        $string = md5($str);
        return substr($string, 0, $number);
    }

    /**
     * 生成guid形式的随机码
     * @return type
     */
    public static function getGUID() {
        return sprintf(
            '%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)
        );
    }
}